/**
 * Created by wangfan on 2017/5/13.
 */

// $(document).ready(function() {
//     $('#houseTable').DataTable();
// } );
var editor; // use a global for the submit and return data rendering
$(document).ready(function () {


    editor = new $.fn.dataTable.Editor({
        ajax: {
            create: {
                type: 'POST',
                url: '/commis/api/user/create'
            },
            edit: {
                type: 'PUT',
                url: '/commis/api/user/edit?id=_id_'
            },
            remove: {
                type: 'DELETE',
                url: '/commis/api/user/remove?id=_id_'
            }
        },
        i18n: {
            create: {
                title: "新增记录",
                submit: "添加"
            },
            edit: {
                title: "修改记录",
                submit: "修改",
                confirm: "您确定要修改这 %d 条记录吗?"
            },
            remove: {
                title: "删除记录",
                submit: "删除",
                confirm: "您确定要删除这 %d 条记录吗?"
            }
        },
        idSrc: 'userId',
        table: "#userTable",
        fields: [
            {
                label: "登录名:",
                name: "userName"
            }, {
                label: "用户名称:",
                name: "realName"
            },
            {
                label: "邮箱:",
                name: "userEmail"
            },
            {
                label: "电话:",
                name: "userPhone"
            }, {
                label: "所属社区:",
                name: "communityId",
                type: "select"
            },
            {
                label: "所属部门:",
                name: "departId",
                type: "select"
            }, {
                label: "用户类别:",
                name: "userTypeId",
                type: "select"
            }, {
                label: "上级用户:",
                name: "parentId",
                type: "select"
            }, {
                label: "图片名称:",
                name: "image"
            }
        ]
    });

    /*  editor.on('preSubmit', function (e, d, action) {
     console.log(action)
     if (action == "remove") {
     console.log("remove----")
     return true;
     }
     var houseAddField = this.field('houseAdd');
     var timeField = this.field('registerTime');
     var houseNameField = this.field('houseName');
     var houseAreaField = this.field('houseArea');
     var houseTypeField = this.field('houseType');
     var completedTimeField = this.field('completedTime');

     if (houseNameField.val() === '') {
     houseNameField.error('用房名称不能为空');
     return false;
     }
     if (houseAddField.val() === '') {
     houseAddField.error('用房地址不能为空');
     return false;
     }
     var strInputDate = completedTimeField.val();
     if (strInputDate === '') {
     completedTimeField.error('建成时间不能为空');
     return false;
     } else {

     if (strInputDate.indexOf("-") <= 0) {
     completedTimeField.error('建成时间请输入合法的日期格式：yyyy-mm-dd');
     return false;
     }
     strInputDate = strInputDate.replace(/-/g, "/");
     var d = new Date(strInputDate);
     if (isNaN(d)) return false;
     var arr = strInputDate.split("/");
     if (!(parseInt(arr[0], 10) == d.getFullYear()) && (parseInt(arr[1], 10) == (d.getMonth() + 1)) && (parseInt(arr[2], 10) == d.getDate())) {
     completedTimeField.error('建成时间请输入合法的日期格式：yyyy-mm-dd');
     return false;
     }
     }
     strInputDate = timeField.val();
     if (strInputDate === '') {
     timeField.error('填报时间不能为空');
     return false;
     } else {

     if (strInputDate.indexOf("-") <= 0) {
     timeField.error('填报时间请输入合法的日期格式：yyyy-mm-dd');
     return false;
     }
     strInputDate = strInputDate.replace(/-/g, "/");

     var d = new Date(strInputDate);
     if (isNaN(d)) return false;
     var arr = strInputDate.split("/");
     if (!(parseInt(arr[0], 10) == d.getFullYear()) && (parseInt(arr[1], 10) == (d.getMonth() + 1)) && (parseInt(arr[2], 10) == d.getDate())) {
     timeField.error('填报时间请输入合法的日期格式：yyyy-mm-dd');
     return false;
     }

     }

     if (houseAreaField.val() === '') {
     houseAreaField.error('面积不能为空');
     return false;
     }
     if (houseTypeField.val() === '') {
     houseTypeField.error('用房类别不能为空');
     return false;
     }

     return true;
     });
     */
    // var serverParams =
    //     {
    //         houseAdd: "",
    //         completedTime: "",
    //         registerTime: "",
    //         houseType: "",
    //     };
    var table = $('#userTable').DataTable({

        dom: "Bfrtip",
        ajax: "/commis/api/user/pageDatatables",
        autoWidth:true,
        responsive: true,

        serverSide: true,
        processing: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        searching: false,//是否开始本地搜索
        stateSave: false,//刷新时是否保存状态
        autoWidth: true,//自动计算宽度
        language: {
            emptyTable: "没有数据",
            search: "搜索:",
            paginate: {
                first: "首页",
                previous: "上一页",
                next: "下一页",
                last: "尾页"

            },
            select: {
                rows: {
                    _: "选中 %d 行",
                    1: "选中 1 行"
                }/*,
                 columns: "选中 %d 列"*/
            },
            lengthMenu: "每页 _MENU_ 条记录",
            zeroRecords: "没有找到记录",
            info: "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )，共 _TOTAL_ 项",
            infoEmpty: "无记录",
            infoFiltered: "(从 _MAX_ 条记录过滤)"
        },
        columns: [
            {
                data: 'userName', defaultContent: ""
            }, {
                data: 'realName', defaultContent: ""
            }, {
                data: 'userEmail', defaultContent: ""
            },{
                data: 'userPhone', defaultContent: ""
            },{
                data: 'community.communityName', defaultContent: ""
            }, {
                data: 'depart.departName', defaultContent: ""

            }, {
                data: 'userType.userTypeName', defaultContent: ""
            },{
                data: 'parentUser.realName', defaultContent: ""
            },{
                data: 'image', defaultContent: ""
            }], select: true,
        buttons: [
            {extend: "create", editor: editor, text: "添加", className: 'btn btn-primary'},
            {extend: "edit", editor: editor, text: "修改", className: 'btn btn-primary'},
            {extend: "remove", editor: editor, text: "删除", className: 'btn btn-danger'}
        ]

    });


    $('#search').on('click', function () {

        var url = "/commis/api/user/pageDatatables?1=1"

        if ($("#userName").val() != "") {
            var userName = $('#userName').val();
            url = url + "&userName=" + userName;
        }
        if ($("#realName").val() != "") {
            var realName = $('#realName').val();
            url = url + "&realName=" + realName;
        }


        table.ajax.url(url).load();
    });


    $('#userTable').on('change', 'input.editor-active', function () {
        editor
            .edit($(this).closest('tr'), false)
            .set('active', $(this).prop('checked') ? 1 : 0)
            .submit();
    });


})
//设置框架
$(function () {
    $("#index_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});